#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@Time    :   2021/08/25 21:33:11
@Author  :   Leo Wood 
@Contact :   leowood@foxmail.com
'''

from re import S
from bs4 import BeautifulSoup

import os

import re

def get_words_from_dirs():
    words = []
    for root,dirs,files in os.walk("MESH/html"):
        words = files


    with open('mesh_crawed.txt','w',encoding='utf-8') as f:
        for word in words:
            f.write(word.replace('.html','') + '\n')




def get_similar_words_from_html(path):

    # word = file_name.split(".")[0]
    word_dict = {"similar_words":[],"definition":'','description':''}

    with open(path,'r',encoding='utf-8') as f:
        soup = BeautifulSoup(f.read(), "html.parser")

        description_soup = soup.find('div',class_='alert-primary')
        if description_soup:
            description = description_soup.get_text().strip()
            word_dict['description'] = description

        definition_soup = soup.find(text=re.compile(r'.*基本释义.*'))
        if definition_soup:
            definition = ''
            if '[' in definition_soup.parent.parent.next_sibling.next_sibling.string and '[英' not in definition_soup.parent.parent.next_sibling.next_sibling.string:
                definition_tag = definition_soup.parent.parent.next_sibling.next_sibling.next_sibling.next_sibling
                definition = definition_tag.string.strip()
            else:
                definition_tag = definition_soup.parent.parent.next_sibling.next_sibling
                definition = definition_tag.string.strip()
            while True:
                if definition_tag.next_sibling.next_sibling.string:
                    definition += definition_tag.next_sibling.next_sibling.string.strip()
                    definition_tag = definition_tag.next_sibling.next_sibling
                else:
                    break

            word_dict['definition'] = definition

        similar_words_soup = soup.find(text=re.compile(r'.*的相似词.*'))

        if similar_words_soup:
            similar_words_soup = similar_words_soup.parent.parent.next_sibling.next_sibling
            for child in similar_words_soup.children:
                if ' ' in child.string:
                    for w in child.string.split():
                        word_dict['similar_words'].append(w)
                elif child.string.strip():
                     word_dict['similar_words'].append(child.string.strip())

    print(word_dict)

if __name__ == '__main__':
    # get_similar_words_from_html(r'F:\LiuHuan\Projects\Spiders\KMcha\MESH\html\安道尔.html')
    # get_similar_words_from_html(r'F:\LiuHuan\Projects\Spiders\KMcha\MESH\html\髋.html')
    get_similar_words_from_html(r'F:\LiuHuan\Projects\Spiders\KMcha\MESH\html\氨.html')


    